/*
 * @Author: Islands 1430536748@qq.com
 * @Date: 2024-07-13 01:49:05
 * @LastEditors: 周洋 1430536748@qq.com
 * @LastEditTime: 2024-08-10 14:58:43
 * @version: 1.1.4
 * @Description: vite 全局配置
 */
import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import { resolve } from 'path';
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import ElementPlus from 'unplugin-element-plus/vite';
import rulesUnocss from './src/plugins/unocss/uno.config';
import UnoCSS from 'unocss/vite';
import path from 'path'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import Inspect from 'vite-plugin-inspect';

const pathSrc = path.resolve(__dirname, 'src')

export default defineConfig(async ({ command, mode }) => {
  const env = loadEnv(mode, process.cwd(), '');
  return {
    presets: [
      '@vue/cli-plugin-babel/preset'
    ],
    plugins: [
      vue(),
      ElementPlus({ useSource: true }),
      AutoImport({
        imports: ['vue'],
        resolvers: [
          ElementPlusResolver(),
          // 自动导入图标组件
          IconsResolver({
            prefix: 'Icon',
          }),
        ],
        dts: path.resolve(pathSrc, 'auto-imports.d.ts'),
      }),
      Components({
        // 允许子目录作为组件的命名空间前缀
        directoryAsNamespace:true,  
        resolvers: [
          ElementPlusResolver({ importStyle: 'sass' }),
          // 自动注册图标组件
          IconsResolver({
            enabledCollections: ['ep'],
          }),
        ],
        dts: path.resolve(pathSrc, 'components.d.ts'),
      }),
      Icons({
        autoInstall: true,
      }),
      Inspect(),
      UnoCSS({
        rules:rulesUnocss.rules
      }),
      // error ---> 这里打开会导致打包错误 Cannot create property 'name' on string 'import'；暂时没找到解决方案
      // "import",
      // {
      //   libraryName: '@icon-park/vue',
      //   libraryDirectory: 'es/icons',
      //   camel2DashComponentName: true // default: true,
      // }
    ],
    //运行环境/代理配置
    server: {
      host: env.VITE_HOST,
      port: Number(env.VITE_PORT),
      open: true,
    },
    //别名配置
    resolve: {
      alias: [
        { find: '@', replacement: resolve(__dirname, '/src') },
        { find: '@mock', replacement: resolve(__dirname, './mock') },
      ]
    },
    //css预处理器 sass;
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: ` @use "@/assets/css/globals.scss" as *; 
                            @use "@/assets/css/mixin/index.scss" as *;
                            @use "@/assets/css/fit/index.scss" as *;
                            @use "@/assets/css/element-plus/light.scss" as *;
                            @use "element-plus/theme-chalk/src/dark/css-vars.scss" as *;`
        }
      },
    },
  }
})
